package com.beiye.drivertransport.SubActivity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.adapter.LearnPagerAdapter;
import com.beiye.drivertransport.illegallearningfragment.AdditionalCoursesFragment;
import com.beiye.drivertransport.illegallearningfragment.ClassLearningFragment;
import com.githang.statusbar.StatusBarCompat;

import java.lang.reflect.Field;
import java.util.ArrayList;



//违章学习列表
public class SubIllegalLearningActivity extends TwoBaseAty {


    TabLayout TabLayout1;

    TextView title;
    private int TAB_MARGIN_DIP = 35;

    ViewPager myviewpager;
    //    //所有标题按钮的数组
//    private TextView[] btnArgs;
    private ArrayList<Fragment> fragments;
    private LearnPagerAdapter adapter;
    private ArrayList<String> list_title;


    @Override
    public int getLayoutId() {
        return R.layout.activity_sub_illegal_learning;

    }

    @Override
    public void initData() {
        TabLayout1 = (TabLayout) findViewById(R.id.tab_FindFragment_title1);
        title = (TextView) findViewById(R.id.title);
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        ImageView img_back = (ImageView) findViewById(R.id.img_illegalback);
        //返回首页
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



        Bundle bundle = getIntent().getExtras();
        int uvtSn = bundle.getInt("uvtSn");
        String jumpType = bundle.getString("jumpType");

        if (jumpType.equals("homeFgt")) {
            title.setText("违章学习");
        }else{
            title.setText("信用修复课程学习");
        }
        //    int mark = bundle.getInt("mark");
        fragments = new ArrayList<Fragment>();
        ClassLearningFragment classLearningFragment = new ClassLearningFragment();
        fragments.add(classLearningFragment);
        AdditionalCoursesFragment additionalCoursesFragment = new AdditionalCoursesFragment();
        fragments.add(additionalCoursesFragment);
        Bundle args1 = new Bundle();
        args1.putInt("uvtSn", uvtSn);
        //  args1.putInt("mark", mark);
        classLearningFragment.setArguments(args1);
        Bundle args2 = new Bundle();
        args2.putInt("uvtSn", uvtSn);
        //  args2.putInt("mark", mark);
        additionalCoursesFragment.setArguments(args2);

        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("学习类别");
        list_title.add("附加课程");
        TabLayout1.setTabMode(TabLayout.MODE_FIXED);
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(0)));
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(1)));
        adapter = new LearnPagerAdapter(getSupportFragmentManager(), fragments, list_title);
        myviewpager.setAdapter(adapter);
        TabLayout1.setupWithViewPager(myviewpager);
        setIndicator(SubIllegalLearningActivity.this, TabLayout1, TAB_MARGIN_DIP, TAB_MARGIN_DIP);


    }

    //修改tablayout的tabIndicator宽度
    public void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field slidingTabIndicator = null;
        try {
            slidingTabIndicator = tabLayout.getDeclaredField("slidingTabIndicator");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        slidingTabIndicator.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) slidingTabIndicator.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    public DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    public float getPXfromDP(float value, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics());
    }

    @Override
    public void requestData() {

    }
}
